package com.lovo.project.service.impl;
import com.lovo.project.dao.PetDAO;
import com.lovo.project.enums.PetEnum;
import com.lovo.project.exception.OurException;
import com.lovo.project.model.PetDO;
import com.lovo.project.service.PetService;
import com.lovo.project.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import java.util.List;

/**
 * @author 955
 * @title 宠物业务实现类
 * @date 2020/11/3 20:24
 */
public class PetServiceImpl implements PetService {
    @Override
    public void insertPet(PetDO pet) {
        SqlSession sqlSession= MyBatisUtil.getSqlSession();
        PetDAO dao=sqlSession.getMapper(PetDAO.class);
        try{
            int isInsert=dao.insertPet(pet);
            if(isInsert<=0){
                throw new OurException(PetEnum.PET_INSERTEXP_ENUM);
            }
            sqlSession.commit();
        }catch (OurException o){
            sqlSession.rollback();
            throw o;
        }finally {
            sqlSession.close();
        }
    }

    @Override
    public void deletePet(Integer id) {
        SqlSession sqlSession= MyBatisUtil.getSqlSession();
        PetDAO dao=sqlSession.getMapper(PetDAO.class);
        try{
            int isDel=dao.deletePet(id);
            if(isDel<=0){
                throw new OurException(PetEnum.PET_DELETEEXP_ENUM);
            }
            sqlSession.commit();
        }catch (OurException o){
            sqlSession.rollback();
            throw o;
        }finally {
            sqlSession.close();
        }
    }

    @Override
    public PetDO selectById(Integer id) {
        SqlSession sqlSession= MyBatisUtil.getSqlSession();
        PetDAO dao=sqlSession.getMapper(PetDAO.class);
        try{
            PetDO pd=dao.selectById(id);
            if(pd==null){
                throw new OurException(PetEnum.PET_FINDEXP_ENUM);
            }
            return pd;
        }catch (OurException o){
            throw o;
        }finally {
            sqlSession.close();
        }
    }

    @Override
    public List<PetDO> selectAll(String type, String value) {
        SqlSession sqlSession= MyBatisUtil.getSqlSession();
        PetDAO dao=sqlSession.getMapper(PetDAO.class);
        try{
           List<PetDO>list=dao.selectAll(type,value);
            if(list==null){
                throw new OurException(PetEnum.PET_FINDEXP_ENUM);
            }
            return list;
        }catch (OurException o){
            throw o;
        }finally {
            sqlSession.close();
        }
    }

    @Override
    public void update(PetDO pet) {
        SqlSession sqlSession= MyBatisUtil.getSqlSession();
        PetDAO dao=sqlSession.getMapper(PetDAO.class);
        try{
           int isUpdate=dao.update(pet);
            if(isUpdate<=0){
                throw new OurException(PetEnum.PET_FINDEXP_ENUM);
            }
            sqlSession.commit();
        }catch (OurException o){
            sqlSession.rollback();
            throw o;
        }finally {
            sqlSession.close();
        }
    }
    @Override
    public List<PetDO> byPetOwnerIdSelectPet(String type,String value,Integer id) {
        SqlSession sqlSession= MyBatisUtil.getSqlSession();
        PetDAO dao=sqlSession.getMapper(PetDAO.class);
        try{
            List<PetDO>list=dao.byPetOwnerIdSelectPet(type,value,id);
            if(list==null){
                throw new OurException(PetEnum.PET_FINDEXP_ENUM);
            }
            return list;
        }catch (OurException o){
            throw o;
        }finally {
            sqlSession.close();
        }
    }
}
